package edu.gatech.cs2340.teamrocket.control.guicontrol;

import java.util.Observable;
import javax.swing.JOptionPane;
import edu.gatech.cs2340.teamrocket.control.exception.InvalidNameException;
import edu.gatech.cs2340.teamrocket.model.Model;
import edu.gatech.cs2340.teamrocket.model.team.Team;
import edu.gatech.cs2340.teamrocket.model.job.*;
import edu.gatech.cs2340.teamrocket.view.GameFrame;
import edu.gatech.cs2340.teamrocket.view.IntroScreenGUI;
import java.util.Observer;

/**
 * Initialize the game when the IntroScreenGUI is up
 *
 * @author Wai Chip Ngai
 *
 */
public class IntroScreenControl {
    private GameFrame gf;
    private IntroScreenGUI introGUI;
    private String userName;
    private String[] pokeNames;
    private Model model;

    /**
     * To wagon and the visual representation set up screen.
     * @param gf the main Game Frame
     */
    public IntroScreenControl(GameFrame gf) {
        this.gf = gf;
        pokeNames = new String[Team.NUM_TRAVELERS];
        introGUI = new IntroScreenGUI(this);
        gf.add(introGUI, GameFrame.INTRO_GUI_JPANEL);
        gf.showPanel(GameFrame.INTRO_GUI_JPANEL);
        model = Model.getInstance();
    }

    /**
     * Go back to main menu when the back to menu button is pressed in the gui
     */
    public void backToMenu(){
        gf.showPanel(GameFrame.MAIN_MENU_JPANEL);
    }

    /**
     * Create the main game panel and a shop
     * switch to the main shop panel when the player chooses to start game
     */
    public void begin(){

        //Trying to read user inputs and validate them
        try{
            userName = introGUI.getPlayerName();
            for(int i = 0; i < Team.NUM_TRAVELERS; i++){
                pokeNames[i] = introGUI.getTravelerName(i);
            }
        }catch (InvalidNameException e){
            JOptionPane.showMessageDialog(null, e.getMessage(), "Invalid Name", JOptionPane.ERROR_MESSAGE);
            return;
        }

        assignJob(introGUI.getSelectedJob());
        model.nameTrainer(userName);
        for(int i = 0; i < Team.NUM_TRAVELERS; i++)
            model.namePokemon(i, pokeNames[i]);

        new MainGamePanelControl(gf);
        gf.showPanel(GameFrame.SHOP_GUI_JPANEL);
    }

    /**
     * Set the name for traveler 1
     * @param name the name for the traveler 1
     */
    private void nameTraveler(int i, String name){
        model.namePokemon(i, name);
    }

    /**
     * Change the job the for the main player
     * @param aJob the job for the main player
     */
    private void assignJob(Job job){
        model.setTrainerJob(job);

    }
}
